//
//  CommentModel.m
//  ZhiHuDaily
//
//  Created by albert on 16/8/17.
//  Copyright © 2016年 albert. All rights reserved.
//

#import "CommentModel.h"
#import "CommentRefModel.h"

@implementation CommentModel
{
    CGFloat _cellHeight;
}

+ (NSDictionary *)mj_replacedKeyFromPropertyName{
    return @{
             @"ID" : @"id"
             };
}

- (NSAttributedString *)contentAttr{
    
    if (!_contentAttr) {
        
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
        para.lineSpacing = 5;
        
        _contentAttr = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSParagraphStyleAttributeName : para, NSFontAttributeName : [UIFont systemFontOfSize:15]}];
    }
    return _contentAttr;
}

//判断如果包含中文
- (BOOL)containChinese:(NSString *)str {
    
    for (int i = 0; i < [str length]; i++) {
        
        int a = [str characterAtIndex:i];
        
        if (a > 0x4e00 && a < 0x9fff) {
            
            return YES;
        }
    }
    return NO;
}

- (CGFloat)cellHeight{
    //只算一遍
    if (!_cellHeight) {
        
        _cmtRefUnfolded = YES;
        
        CGFloat iconW = 30;
        CGFloat nameLabelH = 18;
        CGFloat timeLabelH = 15;
        UIFont *contentFont = [UIFont systemFontOfSize:15];
        UIFont *contentRefFont = [UIFont systemFontOfSize:14];
        
        NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
        para.lineSpacing = 5;
        
        //计算文字的高度
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - iconW - 28, MAXFLOAT);
        
        //评论内容高度
        CGFloat cmtContentH = [self.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : contentFont, NSParagraphStyleAttributeName : para} context:nil].size.height;
        
        if (cmtContentH - contentFont.lineHeight <= 5) {
            
            if ([self containChinese:self.content]) {
                
                cmtContentH -= 5;
                
                _contentAttr = [[NSAttributedString alloc] initWithString:self.content attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]}];
            }
        }
        
        _contentHeight = ceil(cmtContentH);
        
        //计算cell的高度
        _cellHeight = 10 + nameLabelH + 8 + _contentHeight + 8 + timeLabelH + 10;
        
        // 没有评论引用，直接返回算好的高度
        if (self.reply_to == nil) return _cellHeight;

        // 评论引用已经被删除
        if (self.reply_to.status != 0) {
            
            _cellHeight += (17 + 8);
            
            return _cellHeight;
        }
        
        // 有评论引用且未被删除
        // 评论引用高度
        _cmtRefHeight = self.reply_to.cmtContentRefH;
        
        _cmtRefTowLinesHeight = contentRefFont.lineHeight * 2 + 5;
        
        if (_cmtRefHeight > 50) {
            
            _cmtRefUnfolded = NO;
        }
        
        _cellFoldHeight = _cellHeight + _cmtRefTowLinesHeight + 8;
        
        // 加上引用高度
         _cellHeight += _cmtRefHeight + 8;
    }
    
    return _cellHeight;
}
@end
